﻿Imports System.Windows.Input
Imports System.Data

Public Class VMGestionComptes
    Inherits VMBase
    Implements OMANE.Common.UI.IViewModelTabSection
    Implements ICommand

    Dim _data As DataSet



    Sub New()

    End Sub
    Sub New(ByVal data As DataSet)
        _data = data
    End Sub

    Public Function CanExecute(ByVal parameter As Object) As Boolean Implements System.Windows.Input.ICommand.CanExecute
        Return True
    End Function

    Public Event CanExecuteChanged(ByVal sender As Object, ByVal e As System.EventArgs) Implements System.Windows.Input.ICommand.CanExecuteChanged

    Public Sub Execute(ByVal parameter As Object) Implements System.Windows.Input.ICommand.Execute

    End Sub

    Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
        Get
            Return "pack://application:,,,/OMANE.Common.UI.Theme;component/Images/compass.png"
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public Property Title As String Implements Common.UI.IViewModelTabSection.Title
        Get
            Return "GESTION COMPTES"
        End Get
        Set(ByVal value As String)

        End Set
    End Property

    Public ReadOnly Property TABLE_COMPTES As DataTable
        Get
            If _data.Tables.Contains("TABLE_COMPTES") Then
                Return _data.Tables("TABLE_COMPTES")
            Else
                Return Nothing
            End If
        End Get
    End Property
    Public ReadOnly Property TABLE_POSTES As DataTable
        Get
            If _data.Tables.Contains("TABLE_POSTES") Then
                Return _data.Tables("TABLE_POSTES")
            Else
                Return Nothing
            End If
        End Get
    End Property

    Public Property SelectedCompte As Object


    Public ReadOnly Property AjouterPoste As ICommand
        Get
            Return New SCommand(AddressOf AjouterPosteMethod)
        End Get
    End Property

    Private Sub AjouterPosteMethod(ByVal parameter As Object)
        If SelectedCompte IsNot Nothing Then
            Dim o = 1
            Dim compterv As DataRowView = SelectedCompte
            Dim posterv As DataRowView = parameter

            Dim q = Aggregate elt In _data.Tables("TABLE_DESCCOMPTES").Rows
                    Where CStr(elt("ID_COMPTE")) = CStr(compterv("ID")) _
                    And CStr(elt("ID_POSTE")) = CStr(posterv("ID")) Into Count()


            If q < 1 Then
                _data.Tables("TABLE_DESCCOMPTES").Rows.Add( _
                                compterv("ID"), _
                                posterv("ID"), _
                                "=""""")

            Else
                Dim ui = CRTModule._Application.GetService(Of Common.UI.IUIService)()

                ui.ShowMessage("Ce poste appartient déjà à ce compte")
            End If


        End If

    End Sub

End Class
